package cn.xo68.boot.auth.server.mapper;


import cn.xo68.boot.auth.server.domain.OauthRoleResource;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * OauthRoleResource mybatis mapper
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月28日 11:04:12
 *
 */
public interface OauthRoleResourceMapper {


	@Select("SELECT * FROM oauth_role_resource WHERE serial_id = #{serialId}")
    @Results(value = {
    @Result(id = true, column = "serial_id", property = "serialId"),
        @Result(column = "role_id", property = "roleId"),
        @Result(column = "resource_id", property = "resourceId"),
        @Result(column = "operator_name", property = "operatorName"),
        @Result(column = "operator_id", property = "operatorId"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime")
    })
    OauthRoleResource get(@Param("serialId") String serialId);
	
	@Select("SELECT * FROM oauth_role_resource WHERE serial_id = #{serialId}")
    @Results(value = { 
    @Result(id = true, column = "serial_id", property = "serialId"),
        @Result(column = "role_id", property = "roleId"),
        @Result(column = "resource_id", property = "resourceId"),
        @Result(column = "operator_name", property = "operatorName"),
        @Result(column = "operator_id", property = "operatorId"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime")
    })
    OauthRoleResource find(@Param("serialId") String serialId);

    @Select("SELECT resource_id FROM oauth_role_resource WHERE client_id = #{clientId} and role_id = #{roleId}")
    @Results(value = {
            @Result(column = "resource_id", property = "resourceId")
    })
    List<String> listRoleResourcs(@Param("roleId") String roleId, @Param("clientId") String clientId);
	
	@Select("SELECT * FROM oauth_role_resource")
    @Results(value = { 
    @Result(id = true, column = "serial_id", property = "serialId"),
        @Result(column = "role_id", property = "roleId"),
        @Result(column = "resource_id", property = "resourceId"),
        @Result(column = "operator_name", property = "operatorName"),
        @Result(column = "operator_id", property = "operatorId"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime")
    })
    List<OauthRoleResource> list();
	
	@Insert("insert into oauth_role_resource (serial_id ,role_id, client_id ,resource_id ,operator_name ,operator_id ,create_time ,update_time ) values (#{serialId} , #{roleId}, #{clientId} , #{resourceId} , #{operatorName} , #{operatorId} , #{createTime} , #{updateTime}  )")
    @Options(useGeneratedKeys = true, keyProperty = "serialId")
    void insert(OauthRoleResource oauthRoleResource);
	
	@Update("update oauth_role_resource set  role_id=#{roleId}, resource_id=#{resourceId}, operatior_name=#{operatiorName}, operator_id=#{operatorId}, update_time=#{updateTime} where serial_id = #{serialId}")
    void update(OauthRoleResource oauthRoleResource);

	@Delete("delete FROM oauth_role_resource WHERE client_id = #{clientId} and role_id = #{roleId}")
	void delete(@Param("clientId") String clientId, @Param("roleId")String roleId);
}